package com.AdvancedDemos.LearnVariousAPI.数学计算类;

import java.util.Random;

public class LearnMath {
    public static void main(String[] args) {
        /**
         * Math是数学函数工具类,当中包含众多静态函数
         *
         * public static int/double abs(int/double a) --- 获取参数绝对值
         * public static double ceil(double a) --- 向上取整
         * public static long floor(double a) --- 向下取整
         * public static int round(float a) --- 四舍五入
         * public static int max(int a,int b) --- 获取两个int值中的较大者
         * public static double pow(double a,double b) --- 返回a的b次幂
         * public static double sqrt(double a) --- 返回 double值的正确舍入的正平方根
         * public static double random() --- 返回值为double的随机值,范围[0.0,1.0)包头不包尾
         */


        System.out.println(Math.abs(-12));//12
        System.out.println(Math.abs(123));//123
        System.out.println(Math.abs(-3.14));//3.14

        System.out.println(Math.ceil(4.0001));//5.0
        System.out.println(Math.ceil(4.0));//4.0

        System.out.println(Math.floor(4.999));//4.0
        System.out.println(Math.floor(4.0));//4.0

        System.out.println(Math.round(3.4999));//3
        System.out.println(Math.round(3.5001));//4

        System.out.println(Math.max(10, 20));//20
        System.out.println(Math.min(10, 20));//10

        System.out.println(Math.pow(2, 3));//8.0
        System.out.println(Math.pow(3, 2));//9.0

        System.out.println(Math.random());//0.5737826627975683

        //生成[20,30]之间的随机数
        //使用Random类
        Random rd = new Random();
        System.out.println(rd.nextInt(30 - 20 + 1) + 20);
        //使用Math类
        System.out.println((int) (Math.random() * (30 - 20 + 1)) + 20);
        /*Math.random()-- - [0.0, 1.0)
        Math.random() * (30 - 20 + 1)-- - [0.0, 11.0)
        (int) (Math.random() * (30 - 20 + 1))-- - [0, 10]
        (int) (Math.random() * (30 - 20 + 1)) + 20-- - [20, 30]*/
    }
}
